//
//  EventOverviewController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/27/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "EventOverviewController.h"

@interface EventOverviewController ()

@end

@implementation EventOverviewController

@synthesize tblItems, listData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.listData = [[NSMutableArray alloc] init];
    [self changeStyle];
    [self createNavigation];
    [self loadData];
}

-(void) changeStyle
{
    [StyleControl background:self.view];
    self.tblItems.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tblItems.backgroundColor = [UIColor clearColor];
    self.tblItems.backgroundView = nil;
}
-(void) createNavigation
{
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"buttonlist"] forState:UIControlStateNormal];
    left.frame = CGRectMake(8, 8, 45, 35);
    NSString *title = [NSString stringWithFormat:NSLocalizedString(@"event.overview", @"")];
    [left addTarget:self action:@selector(openEventList) forControlEvents:UIControlEventTouchUpInside];
    [StyleControl naviStyle:self.view andTitle:title andColor:0 andleftButton:left andRightButton:nil borderLeft:1 borderRight:0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) loadData
{
    User *user = [User instance];
    Event *event = [Event instance];
    self.listData = [event getOverviewWS:user._id];
    /*for(int i =0;i<4;i++){
        EventClass *event = [[EventClass alloc] init];
        event.eventTitle = [NSString stringWithFormat:@"title %d",i+1];
        event.totalAmount = 256.45;
        event.amount = 16.34;
        if(i%2)
            event.statusAmount = 1;
        else
            event.statusAmount = 0;
        [self.listData addObject:event];
    }*/
}
-(void)openEventList
{
    EventListController *detail = [[EventListController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Helper *helper = [Helper instance];
    EventOverviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    EventClass *event = [self.listData objectAtIndex:[indexPath row]];
    if( cell == nil){
        cell = [[[EventOverviewCell alloc] initWithStyle:UITableViewCellSeparatorStyleNone reuseIdentifier:@"Cell" statusAmount:event.amount] autorelease];
    }
    [cell.title setText:event.eventTitle];
    [cell.totalAmount setText: [helper stringToMoney:event.totalAmount Currency:event.currency]];
    [cell.amount setText:[helper stringToMoney:event.amount Currency:event.currency]];
    cell.btEdit.tag = [indexPath row];
    [cell.btEdit addTarget:self action:@selector(editEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EventClass *obj = [self.listData objectAtIndex:[indexPath row]];
    Event *event = [Event instance];
    event._id = obj.eventId;
    event._name = obj.eventTitle;
    event.currencyId = obj.currencyId;
    event.currency = obj.currency;
    if(obj.active == 0){
        User *user = [User instance];
        [event activeEvent:user._id eventId:obj.eventId];
    }
    BookingListController *detail = [[BookingListController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
    
}
-(IBAction) editEvent:(id) sender
{
    UIButton *b = (UIButton *) sender;
    EventClass *obj = [self.listData objectAtIndex:b.tag];
    Event *event = [Event instance];
    event._id = obj.eventId;
    event._name = obj.eventTitle;
    event.currencyId = obj.currencyId;
    event.currency = obj.currency;
    BookingDeleteController *detail = [[BookingDeleteController alloc] init];
    //[self.navigationController pushViewController:detail animated:YES];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}
@end
